package com.xunwulian.dubbo.service.impl;

import com.xunwulian.api.iface.RoleAPI;
import com.xunwulian.base.constant.RoleType;
import com.xunwulian.base.dao.RoleDao;
import com.xunwulian.base.entity.Role;
import com.xunwulian.base.util.UserUtil;
import com.xunwulian.dubbo.service.InnerRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liyankai on 2017/3/14.
 */
@Service
public class RoleApiImpl implements RoleAPI {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private InnerRoleService innerRoleService;

    @Override
    public List<Role> getRoleList4Show(String roleId) {
        if (UserUtil.isAdmin(roleId) || UserUtil.isSubAdmin(roleId)) {
            // 列出所有角色
            return roleDao.listAllERRoles();
        } else {
            // 列出所有二级角色
            return roleDao.listSubUserRole(UserUtil.SUBUSER_CODE);
        }
    }

    @Override
    public List getRoleList(String roleId) {
        if (UserUtil.isAdmin(roleId) || UserUtil.isSubAdmin(roleId)) {
            // 列出所有角色
            return roleDao.listAllERRoles();
        } else if (RoleType.COOPER.getType().equals(innerRoleService.getRoleTypeById(roleId).getType())) {
            // 列出所有二级角色
            return roleDao.listSubUserRole(UserUtil.SUBUSER_CODE);
        } else {
            ArrayList list = new ArrayList();
            list.add(roleDao.getRoleById(roleId));
            return list;
        }
    }

    @Override
    public RoleType getRoleTypeById(String roleId) {
        return innerRoleService.getRoleTypeById(roleId);
    }

    /**
     * 增删改查权限
     */
    @Override
    public void updateQX(String operateType, String value, String roleId) {
        if ("add_qx".equals(operateType)) {
            roleDao.add_qx(value, roleId);
        } else if ("del_qx".equals(operateType)) {
            roleDao.del_qx(value, roleId);
        } else if ("edit_qx".equals(operateType)) {
            roleDao.edit_qx(value, roleId);
        } else if ("cha_qx".equals(operateType)) {
            roleDao.cha_qx(value, roleId);
        }
    }
}
